package com.zhangck.bos2_0.action;

import java.sql.Struct;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.print.attribute.standard.Media;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.sun.xml.bind.v2.model.core.ID;
import com.zhangck.bos2_0.Service.FixedAreaService;
import com.zhangck.bos2_0.common.BaseAction;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.crm.domain.Customer;

/** 
* @author 上午茶
* @version 创建时间：2018年5月31日 下午1:17:19 
* 类说明 
*/
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
@Actions
@Controller
public class FixeedAreaAction extends BaseAction<FixedArea> {

	@Autowired
	private FixedAreaService fixeedAreaAction;
	//保存
	@Action(value="fixedAction_sava",results={@Result(name="success", type="redirect", location="/pages/base/fixed_area.html")})
	public String save(){
		System.out.println(11);
		fixeedAreaAction.save(model);
		return  "success";
	}
	
	//分页
		@Action(value="fixedActionPage",results={@Result(name="success",type="json")})
		public String  AreadAction_page(){
			Pageable request = new PageRequest(page-1, rows);
			Specification<FixedArea> specification = new Specification<FixedArea>() {
			
				@Override
				public Predicate toPredicate(Root<FixedArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
					List<Predicate> list = new ArrayList<>();
					//等值查询
					if (StringUtils.isNotBlank(model.getId())) {
						Predicate p1 = cb.equal(root.get("id").as(String.class),
								model.getId());
						list.add(p1);
					}
					//模糊查询
					if (StringUtils.isNotBlank(model.getCompany())) {
						Predicate p2 = cb.like(root.get("company").as(String.class), 
								"%"+model.getCompany()+"%");
						list.add(p2);
					}
					//等值查询
					if (StringUtils.isNotBlank(model.getFixedAreaName())) {
						Predicate p3 = cb.equal(root.get("fixedAreaName").as(String.class),
								model.getFixedAreaName());
						list.add(p3);
					}
					return cb.and(list.toArray(new Predicate[0]));
				}
			};
			
			Page<FixedArea> page1 = fixeedAreaAction.findPageData(specification,request);

			pushPageDataValueStack(page1);
			
			return  "success";
		}				

	//查询未关联的客户
		
		@Action(value="findNoassociatedCustomer_action",results={@Result(name="success",type="json")})
		public String  findNoassociatedCustomer_action(){
				Collection<? extends Customer> collection =
						WebClient.create("http://127.0.0.1:8088/services/customerService/noassocitedCustomer").
						accept(MediaType.APPLICATION_JSON).
						getCollection(Customer.class);	
				ActionContext.getContext().getValueStack().push(collection);
			return "success";
		}
		
		@Action(value="findAssociatedFixedAreaCustomer_action",results={@Result(name="success",type="json")})
		public String  findAssociatedFixedAreaCustomer_action(){
				Collection<? extends Customer> collection =
						WebClient.create("http://127.0.0.1:8088/services/customerService/associatedFixedAreaCustomer/"+model.getId()).
						accept(MediaType.APPLICATION_JSON).
						getCollection(Customer.class);	
				ActionContext.getContext().getValueStack().push(collection);
			return "success";
		}
		
		private  String[] customerIds;
		
		

		public void setCustomerIds(String[] customerIds) {
			this.customerIds = customerIds;
		}
		
		public String[] nocustomerIds;
		

		public void setNocustomerIds(String[] nocustomerIds) {
			this.nocustomerIds = nocustomerIds;
		}

		@Action(value="associatedFixedAreaToCustomer_action",results={@Result(name="success", type="redirect", location="./pages/base/fixed_area.html")})
		public String  associatedFixedAreaToCustomer_action(){
			String customerStr1 = StringUtils.join(customerIds,"-");
			String nocustomerIds1 = StringUtils.join(nocustomerIds,"-");
			if (customerStr1==null) {
				return "success";
			}
					 WebClient.create("http://127.0.0.1:8088/services/customerService/associatedFixedAreaToCustomer?customerStr="+customerStr1+"&nocustomerIds="+nocustomerIds1+"&fixedAreaid="+model.getId())
						.put(null);
			return "success";
		}
		
		private Integer courierId;
		private Integer takeTimeId;

		
		public void setCourierId(Integer courierId) {
			this.courierId = courierId;
		}

		public void setTakeTimeId(Integer takeTimeId) {
			this.takeTimeId = takeTimeId;
		}

		//定区关联快递员
		@Action(value="fixedArea_associationCourierToFixedArea",results={@Result(name="success", type="redirect", location="./pages/base/fixed_area.html")})
		public String  fixedArea_associationCourierToFixedArea(){
			 fixeedAreaAction.associationCourierToFixedArea(model,courierId,takeTimeId);
			return "success";
		}
		
		//定区关联快递员显示
		@Action(value="fixedAreaCourierToFixedArea_association",results={@Result(name="success",type="json")})
		public String  fixedAreaCourierToFixedArea_association(){
			String parameter = ServletActionContext.getRequest().getParameter("id");
			
			Iterable<Integer> couriersId= fixeedAreaAction.CourierToFixedArea_association(parameter);
			
			List<Courier> couriers = fixeedAreaAction.finCouries(couriersId);
			
			 ActionContext.getContext().getValueStack().push(couriers);
			return "success";
		}
		
		
		}
